package com.example.maple.ccsclient.presenter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.example.maple.ccsclient.R;
import com.example.maple.ccsclient.app.PicDetailActivity;
import com.example.maple.ccsclient.interfaces.IPicDetailView;
import com.example.maple.ccsclient.utils.CloseUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by maple on 2016/7/4.
 */
public class PicDetailPresenter {


	private final LayoutInflater layoutInflater;
	private final View view;
	private Context mContext;
	IPicDetailView mIPicDetailView = null;
	private PopupWindow mPopupWindow;
	String charName;
	private int length;

	public PicDetailPresenter(IPicDetailView iPicDetailView, Context context) {
		mContext = context;
		mIPicDetailView = iPicDetailView;
		layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		view = layoutInflater.inflate(R.layout.alert_dialog, null);
	}

	//保存图片
	public void savePic(String url, String name) {
		charName = name;
		DownLoadPicTask task = new DownLoadPicTask();
		task.execute(url);
	}

	public class DownLoadPicTask extends AsyncTask<String, Integer, String> {

		@Override
		protected String doInBackground(String... params) {
			try {
				URL url = new URL(params[0]);
				HttpURLConnection httpURLConnection = null;
				InputStream inputStream = null;
				OutputStream outputStream = null;
				try {
					httpURLConnection = (HttpURLConnection) url.openConnection();
					httpURLConnection.setDoInput(true); //允许输入流，即允许下载
					inputStream = httpURLConnection.getInputStream();
					length = httpURLConnection.getContentLength();
					int hasRead;
					byte[] buffer = new byte[1024];
					//打开文件
					File folder = new File("/sdcard/集字系统/");
					if (!folder.exists()) {
						folder.mkdir();
					}
					Date now = new Date();
					SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_");//可以方便地修改日期格式
					String timeInfo = dateFormat.format(now);
					File f = new File("/sdcard/集字系统/",timeInfo + charName+ ".jpg");
					//File f = new File("/sdcard/CCSPic/", "abv.jpg");
					if (!f.exists()) {
						f.createNewFile();
					}

					outputStream = new FileOutputStream(f);
					while ((hasRead = inputStream.read(buffer)) > 0) {
						if (outputStream != null) {
							outputStream.write(buffer, 0, hasRead);
						}

					}
					Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
					Uri uri = Uri.fromFile(f);
					intent.setData(uri);
					mContext.sendBroadcast(intent);
					return "保存成功";
				} catch (IOException e) {
					e.printStackTrace();
					return "保存失败";
				} finally {
					if (null != httpURLConnection) {
						httpURLConnection.disconnect();
					}
					CloseUtils.closeQuietly(inputStream);
					CloseUtils.closeQuietly(outputStream);
				}
			} catch (MalformedURLException e) {
				e.printStackTrace();
				return "保存失败";
			}


		}

		@Override
		protected void onPostExecute(String s) {
			super.onPostExecute(s);
			mIPicDetailView.showDialog(s);

		}


	}

}
